#include <jni.h>

//
// Created by dukang wang on 2025/1/4.
//

#include "DataQueue.h"
#include "AudioPlayer.h"

using namespace hy;

extern "C"
JNIEXPORT jlong JNICALL
Java_com_hy_opensles_HYOpenSLESPlayer_CreateInstance(JNIEnv *env, jobject thiz) {
    AudioPlayer *audioPlayer = new AudioPlayer();
    long handler = reinterpret_cast<long>(audioPlayer);
    return handler;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_hy_opensles_HYOpenSLESPlayer_Init(JNIEnv *env, jobject thiz, jlong handler) {
    AudioPlayer *audioPlayer = reinterpret_cast<AudioPlayer *>(handler);
    int ret = audioPlayer->Init(44100);
    LOGI("JNI OpenSL ES Player Init ret=%d", ret)
    return ret;
}


extern "C"
JNIEXPORT void JNICALL
Java_com_hy_opensles_HYOpenSLESPlayer_SendPcmData(JNIEnv *env, jobject thiz,
                                                  jlong handler,
                                                  jbyteArray byte_array,
                                                  jint size) {
    AudioPlayer *audioPlayer = reinterpret_cast<AudioPlayer *>(handler);
    jbyte *data = env->GetByteArrayElements(byte_array, nullptr);

    PcmData *pcmData = new PcmData((char *) data, size);
    audioPlayer->putPcmData(pcmData, size);
    env->ReleaseByteArrayElements(byte_array, data, 0);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hy_opensles_HYOpenSLESPlayer_release(JNIEnv *env, jobject thiz, jlong handler) {
    AudioPlayer *audioPlayer = reinterpret_cast<AudioPlayer *>(handler);
    delete audioPlayer;
    audioPlayer=NULL;

}